package com.cictec.middleware.distribute.controller.distribute;

import java.io.Serializable;
import java.util.Map;

import org.mvel2.MVEL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cictec.middleware.commons.utils.DateUtils;
import com.cictec.middleware.commons.utils.Result;
import com.cictec.middleware.commons.utils.ResultUtil;
import com.cictec.middleware.distribute.send.SendHandler;
import com.cictec.middleware.gps.engine.TimingPoint;

/**
 * 中间件分析后数据推送入口
 * 
 * @author Administrator
 *
 */
@RestController
@RequestMapping(value = "/distribute")
public class MiddlewareDistributeController {

	private static Logger logger = LoggerFactory.getLogger(MiddlewareDistributeController.class);

	/**
	 * 设备上传 调度申请信息处理
	 * 
	 * @return
	 */
	@RequestMapping(value = "/sendData")
	public Result sendData(@RequestBody Map data){
		logger.error("【分析数据分发】:{}",data);
//		test(null);
		TimingPoint point = new TimingPoint();
		point.setAttributes(data);
		point.setTime(DateUtils.parseDateTime(point.getGatherTime()));
		
		sendData(point);
		
		logger.error("【分析数据分发】:{}",point);
		return ResultUtil.getSuccessResult();
	}

	private void sendData(TimingPoint busInstance) {


		String str = "isdef GPS_DRIFT && GPS_DRIFT==0";
		Serializable compiled = MVEL.compileExpression(str);
		Boolean result = (Boolean) MVEL.executeExpression(compiled, busInstance.getAttributes());

		if (result) {
			logger.debug("数据推送：【坐标漂移】");
		} else {

			// 判定是否偏线
			if (busInstance.isInDeviate()) {
				logger.debug("数据推送：【数据偏线】");

				// SendHandler.sendBusPostionToMq(busInstance); // 推送至 mq
				SendHandler.sendBusPostionToStomp(busInstance); // 推送至 apollo
				SendHandler.sendBusPostionToMqPlatform(busInstance); // 推送至mq 数据转发平台队列
			} else {

				logger.debug("数据推送：【车辆在线路上的相对位置】");

				SendHandler.sendBusPostionToMq(busInstance); // 推送至 mq
				SendHandler.sendBusPostionToStomp(busInstance); // 推送至 apollo
				SendHandler.sendBusPostionToMqPlatform(busInstance); // 推送至mq 数据转发平台队列
			}
		}
	}
}
